package com.deyuanyun.pic.settings.service.pipe.impl;

import com.deyuanyun.pic.common.cache.TempStoreDataSupport;
import com.deyuanyun.pic.settings.controller.dto.PipeValidateVO;
import com.deyuanyun.pic.settings.controller.dto.PipeValveRoomInfraVO;
import com.deyuanyun.pic.settings.controller.dto.ValveRoomDTO;
import com.deyuanyun.pic.settings.domain.pipe.ValveRoom;
import com.deyuanyun.pic.settings.domain.util.DomainInitUtil;
import com.deyuanyun.pic.settings.mapper.pipe.ValveRoomMapper;
import com.deyuanyun.pic.settings.service.pipe.ValveRoomService;
import com.deyuanyun.pic.settings.support.utils.PipeDictUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ValveRoomServiceImpl implements ValveRoomService {
	
	@Resource
	private ValveRoomMapper valveRoomMapper;

	/**
	* @Description: 新建站场阀室，包括：站场阀室名称、站场阀室代号
	* @author h.x.rong
	* @date 2015年12月23日
	* @param @param PipeValveRoomInfraVO
	* @return void 
	*
	 */
	public void insertValveRoom(PipeValveRoomInfraVO pipeValveRoomInfraVO) {
        DomainInitUtil.initCreateDomain(pipeValveRoomInfraVO);
		pipeValveRoomInfraVO.setTaskStatus("03");
		pipeValveRoomInfraVO.setStatus((short)1);
		valveRoomMapper.insertValveRoom(pipeValveRoomInfraVO);
		
	}

	/**
	 * 条件查询，包括：站场阀室名称、站场阀室代号，任务状态、状态
	 * @param
	 * @return
	 * @author axiao(xj08.dream@gmail.com)
	 * @date 2016-09-01
	 */
	public List<ValveRoomDTO> findByCondition(PipeValveRoomInfraVO pipeValveRoomInfraVO) {
		return valveRoomMapper.findByCondition(pipeValveRoomInfraVO);
	}

	public int deleteByIds(String[] ids) {
        return valveRoomMapper.deleteByIds(ids);
	}

	/**
	 * 清空场站阀室缓存
	 * @param
	 * @return
	 * @author axiao(xj08.dream@gmail.com)
	 * @date 2016-11-18
	 */
	private void clearVavleRoomCache() {
		TempStoreDataSupport.removeData(PipeDictUtils.PIPEDICTUTILS_VALVEROOMBIMAP);
        TempStoreDataSupport.removeData(PipeDictUtils.PIPEDICT_GETROOMS);
	}

    /**
	* @Description: 批量修改状态（发布）
	* @author h.x.rong
	* @date 2015年12月23日
	* @param 
	* @return void 
	 */
	public int updateStatus(String[] ids) {
		int n = valveRoomMapper.updateStatus(ids);
		if(n > 0) {
			clearVavleRoomCache();
		}
		return n;
	}

	public int updateVRS(String[] ids, String status) {
		int n = valveRoomMapper.updateVRS(ids, status);
        if(n > 0) {
            clearVavleRoomCache();
        }
        return n;
	}

	/**
	* @Description: 站场阀室修改、编辑
	* @author h.x.rong
	* @date 2015年12月31日
	* @param @param pipeValveRoomInfraVO
	* @return void 
	*
	 */
	public void updateVR(PipeValveRoomInfraVO pipeValveRoomInfraVO) {
		valveRoomMapper.updateVR(pipeValveRoomInfraVO);
	}

	public boolean validateRepByVR(PipeValidateVO pipeValidateVO) {
		int n = valveRoomMapper.validateRepByVR(pipeValidateVO);
        if(n == 0)
            return false;
        return true;
	}

	@Override
	public List<ValveRoom> queryAllValueRoomList(Map map){
		return valveRoomMapper.queryAllValueRoomList(map);
	}


    @Override
    public Map queryValueRoomListByLineId(String lineId) {
        Map<String,Object> result=new HashMap<String,Object>();
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("lineId",lineId);
        param.put("type","begin");

        result.put("begin",valveRoomMapper.queryValueRoomListByLineId(param));
        param.put("type","end");
        result.put("end",valveRoomMapper.queryValueRoomListByLineId(param));
        return result;
    }

	@Override
	public List<ValveRoomDTO> queryAllValueRooms(String name) {
		return valveRoomMapper.queryAllValueRooms(name);
	}
	@Cacheable("tempStoreData")
	public ValveRoomDTO getValueRoomByName(String name) {
		return valveRoomMapper.getValueRoomByName(name);
	}
    @Override
    public ValveRoomDTO selectByPrimaryKey(String id) {
        return valveRoomMapper.selectByPrimaryKey(id);
    }


    @Override
    @Cacheable("tempStoreData")
    public ValveRoomDTO selectByPrimaryKeyCache(String id) {
        return valveRoomMapper.selectByPrimaryKey(id);
    }

	@Override
	public List<Map> queryValueRoom(String lineId, BigDecimal startMileage, BigDecimal endMileage) {
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("lineId", lineId);
		param.put("startMileage", startMileage);
		param.put("endMileage", endMileage);
		return valveRoomMapper.queryValueRoom(param);
	}

}
